package com.yixun.qfbao.hander;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.apidto.AddFlowRecordReqDto;
import com.yixun.qfbao.apidto.AddFlowRecordResDto;
import com.yixun.qfbao.model.dto.FlowRecordDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.ObjectTypeEnum;
import com.yixun.qfbao.model.enums.PlatformEnum;
import com.yixun.qfbao.service.FlowRecordService;
import com.yixun.qfbao.vo.AddFlowRecordReqVo;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.PremisesService;

@Service("addFlowRecordHander")
public class AddFlowRecordHander {
	
	private static final Logger logger = LoggerFactory.getLogger(AddFlowRecordHander.class);

	@Autowired
	private FlowRecordService flowRecordService;
	@Autowired
	private PremisesService premisesService;

	public AddFlowRecordResDto hander(AddFlowRecordReqVo reqDto) {
		AddFlowRecordReqDto addFlowRecordReqDto = reqDto.getParams();
		AddFlowRecordResDto resDto = new AddFlowRecordResDto();
		try {
			Long higherId = null;
			if (null != addFlowRecordReqDto.getObjectId()
					&& ObjectTypeEnum.PREMISES.getCode().equals(addFlowRecordReqDto.getObjectType())) {
				// 楼盘统计类型且存在楼盘Id,获取楼盘数据所在的公司
				Premises premises = premisesService.selectSimpleById(addFlowRecordReqDto.getObjectId());
				if (null != premises) {
					higherId = premises.getCompanyId();
				}
			}

			FlowRecordDto entity = new FlowRecordDto();
			entity.setHigherId(higherId);
			entity.setPlatform(PlatformEnum.V10.getCode());
			entity.setVisitor(addFlowRecordReqDto.getVisitor());
			entity.setVisitorName(addFlowRecordReqDto.getVisitorName());
			entity.setVisitorCompanyUuid(addFlowRecordReqDto.getVisitorCompanyUuid());
			entity.setVisitorCompany(addFlowRecordReqDto.getVisitorCompany());
			entity.setObjectId(addFlowRecordReqDto.getObjectId());
			entity.setObjectType(addFlowRecordReqDto.getObjectType());
			entity.setFirType(addFlowRecordReqDto.getFirType());
			entity.setSecType(addFlowRecordReqDto.getSecType());
			entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
			flowRecordService.create(entity);
			resDto.setResponseCode(ResultEnum.SUCCESS.getNo());
		} catch (Exception e) {
			logger.error("addFlowRecordHander err: {}", e);
			resDto.setResponseCode(ResultEnum.FAIL.getNo());
			resDto.setResponseMessage("添加流量信息失败");
		}
		return resDto;
	}
}
